/**
 * 编辑车辆
 */
layui.use(['form', 'admin', 'HttpRequest','layer','table'], function () {
    var form = layui.form;
    var admin = layui.admin;
    var HttpRequest = layui.HttpRequest;
    var $ = layui.$;
    var layer = layui.layer;


    // 获取设备详情
    // var request = new HttpRequest(Feng.ctxPath + "/deviceconfig/detail?id=" + Feng.getUrlParam("id"), 'get');
    // var result = request.start();
    // result.data.createTime = timetrans(result.data.createTime);
    // result.data.deviceLastTime = timetrans(result.data.deviceLastTime);
    // result.data.deviceEntryTime = timetrans(result.data.deviceEntryTime);
    // form.val('deviceConfigForm', result.data);


    // 设备类型下拉框回显
    layui.use(['form', 'upload', 'layer','laydate'],function(){
        var laydate = layui.laydate;
        var form = layui.form;
        //执行一个laydate实例
        laydate.render({
            elem: '#test1', //指定元素
        });
        deviceBrand()
        deviceType()
    });

    // 点击选择项目按钮
    $('#btn_pro').click(function () {
        layui.use('table' ,function () {
            var table = layui.table;
            layer.open({
                type: 2,
                area: ["700px", '430px'],
                title: "选择项目",
                maxmin: false,
                content:Feng.ctxPath + '/deviceconfig/manage/editProject',
                success: function (index, layero) {

                }
            });
        });
    });
    // 点击选择设备按钮
    $('#btn_device').click(function () {
        layui.use('table' ,function () {
            var table = layui.table;
            layer.open({
                type: 2,
                area: ["700px", '430px'],
                title: "选择设备",
                maxmin: false,
                content:Feng.ctxPath + '/deviceconfig/manage/editDevice',
                success: function (index, layero) {

                }
            });
        });
    });
    // 点击选择摄像头
    $('#btn_Camera').click(function () {
        layui.use('table' ,function () {
            var table = layui.table;
            layer.open({
                type: 2,
                area: ["700px", '430px'],
                title: "选择设备",
                maxmin: false,
                content:Feng.ctxPath + '/deviceconfig/manage/editCamera',
                success: function (index, layero) {

                }
            });
        });
    });





    function deviceBrand() {
        $.ajax({
            url: Feng.ctxPath + '/device/brandList',
            dataType: 'json',
            type: 'get',
            success: function (data) {
                //使用循环遍历，给下拉列表赋值
                $.each(data.data, function (index, value) {
                    var option = new Option(value.name,value.devcieBrandId);
                    $('#deviceBrand').append(option);// 下拉菜单里添加元素
                });
                // 遍历select

                // form.render("select")
                // layui.form.render("select");//重新渲染 固定写法
            }
        })
    }

    function deviceType() {
        $.ajax({
            url: Feng.ctxPath + '/dict/getDictListPage',
            data:{page:"1",limit:"10",dictTypeCode:"deviceType"},
            dataType: 'json',
            type: 'get',
            success: function (data) {
                //使用循环遍历，给下拉列表赋值
                // 先不渲染,等选中后再渲染
                $.each(data.data.rows, function (index, value) {
                    var option = new Option(value.dictName,value.dictCode);
                    $('#deviceType').append(option);// 下拉菜单里添加元素
                });
            }
        })
    }




    function timetrans (res) {
        // 时间戳为0的情况下util.toDateString转换失败
        var date = new Date(res*1000);//如果date为13位不需要乘1000
        var Y = date.getFullYear() + '-';
        var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
        var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
        var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
        var m = (date.getMinutes() <10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
        var s = (date.getSeconds() <10 ? '0' + date.getSeconds() : date.getSeconds());
        return Y+M+D+h+m+s;
    };


    // 表单提交事件
    form.on('submit(btnSubmitConfig)', function (data) {
        var request = new HttpRequest(Feng.ctxPath + "/deviceconfig/editOrAdd", 'post', function (data) {
            console.log(111)
            Feng.success("新增成功!");
            admin.putTempData('formOk', true);
            admin.closeThisDialog();
            parent.location.reload();
        }, function (data) {
            console.log(222)
            Feng.error("新增失败!" + data.message);
            // admin.closeThisDialog();
        });
        request.set(data.field);
        request.start(true);
    });

});